var ConsultantReserve = {
    id: "ConsultantReserveTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

ConsultantReserve.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 130},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.customerAge == "" || row.customerAge == null || row.customerAge == "null") {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }else if(row.customerAge >= 0 && row.customerAge < 18){
                    if(row.customerSex == 1){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }else if(row.customerSex == 2){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }else {
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }
                }else if(row.customerAge >= 18 && row.customerAge <= 50){
                    if(row.customerSex == 1){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }else if(row.customerSex == 2){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }else {
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }
                }else if(row.customerAge > 50){
                    if(row.customerSex == 1){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }else if(row.customerSex == 2){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }else {
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantReserve.consultantReserveToDetail(" + '"' + row.id + '"' + ")' ><img  src='https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    }
                }
            }
        },
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
        {title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                if (value == 1) {return "男";} else if (value == 2) {return "女"} else {return "未知"}
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '实际余额', field: 'actualAccountBalance', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '欠费', field: 'accountarrears', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter:function (value, row, index) {
                return "<span style='color: red;'>"+row.accountarrears+"</span>"
            }
        },
        {title: '看诊状态', field: 'diagnosisStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (row.diagnosisContent == "" || row.diagnosisContent == null) {
                    row.diagnosisContent = "无看诊详情";
                } else {
                    var reg = /@,/g;
                    row.diagnosisContent = "@," + "看诊详情：" + "@," + row.diagnosisContent
                    row.diagnosisContent = row.diagnosisContent.replace(reg, '&#10;');
                    var regs = /@/g;
                    row.diagnosisContent = row.diagnosisContent.replace(regs, '');
                }
                if (value == "已看诊") {
                    return "<a href='javascript:;' onclick='ConsultantReserve.consultantReserveToDetailkz(" + '"' + row.id + '"' + ")'>" +
                        "<span style=' cursor:pointer' title='" + row.diagnosisContent + "'>[已看诊]</span></a>"
                } else {
                    return "<a href='javascript:;' onclick='ConsultantReserve.consultantReserveToDetailkz(" + '"' + row.id + '"' + ")'>" +
                        "<span style=' cursor:pointer' title='" + row.diagnosisContent + "'>未看诊</span></a>"
                }
            }
        },
        {
            title: '开单状态', field: 'dealStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                function leftTrim(str)
                {
                    return str.replace(/^\s*/g,"");
                }
                if (row.huifang == "" || row.huifang == null) {
                    row.huifang = "无回访详情";
                } else {
                    row.huifang = ",<b>" + "回访详情：" + row.huifang.replace(/\s{2,}/g,"");
                    var reg = /,<b>/g;
                    var regs = /<\/b>/g;
                    row.huifang = row.huifang.replace(reg, '&#10;');
                    row.huifang = row.huifang.replace(regs, ' ');
                }
                if (value == "已结账") {
                    return "<a href='javascript:;' onclick='ConsultantReserve.consultantReserveToDetailhf(" + '"' + row.id + '"' + ")'>" +
                        "<span style=' cursor:pointer' title='" + row.huifang + "'>[已结账]</span></a>"
                } else {
                    return "<a href='javascript:;' onclick='ConsultantReserve.consultantReserveToDetailhf(" + '"' + row.id + '"' + ")'>" +
                        "<span style=' cursor:pointer' title='" + row.huifang + "'>未成交</span></a>"
                }
            }
        },
        {title: '成交状态', field: 'isdealStatus', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {title: '回访人', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 90},
        {title: '回访时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '初诊时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '到店时间', field: 'arrivalTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '看诊详情', field: 'diagnosisContent', align: 'center', valign: 'middle', sortable: true, width: 200,
            formatter: function (value, row, index) {
                return '<span title="'+value+'">'+value+'</span>'
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
            }
        },
        {title: '赠送余额', field: 'giveAccountBalance', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true, width: 110}
    ];
    return columns;
};

ConsultantReserve.consultantReserveToDetailkz = function (id) {//点击进入看诊详情
    var index = layer.open({
        type: 2,
        title: '操作',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id+'/4/1'
    });
    this.layerIndex = index;
};

ConsultantReserve.consultantReserveToDetailhf = function (id) {//点击开单状态进入详情
    var index = layer.open({
        type: 2,
        title: '操作',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id+'/3/1'
    });
    this.layerIndex = index;
};

ConsultantReserve.consultantReserveToDetail = function (id) {//点击姓名进入详情
    var index = layer.open({
        type: 2,
        title: '&nbsp',
        area: ['100%', '100%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/'+id+'/4/1'
    });
    this.layerIndex = index;
};

ConsultantReserve.opendealStatus = function (id) {
    $.ajax({
        url: Feng.ctxPath + "/reservations/getTotleMoneys",
        type: "post",
        data: {
            "id": id
        },
        success: function (data) {
            alert("成交金额：" + data + "元");
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
};

ConsultantReserve.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ConsultantReserve.seItem = selected[0];
        return true;
    }
};

ConsultantReserve.totalNum = function () {//统计总人数 获取跟踪人数
    var param1 = $("#startTime").val();  //开始预约时间
    var param2 = $("#endTime").val();   //结束预约时间
    var param3 = $("#followStatus").val();  // 跟踪状态
    var param4 = $("#consumptionStatus").val(); //消费状态
    var param5 = $("#fieldTc").val();
    var param6 = $("#dealStatus").val();
    var param7 = $("#diagnosisStatus").val();
    var param8 = parent.$("#doctorTcInputPublic").val();
    var param9= $("#isDone").val();
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/reserve/totalNum",
        data: {
            param1: param1, param2: param2, param3: param3,
            param4: param4,param5: param5,param6: param6,
            param7: param7,param8: param8,param9: param9
        },
        dataType: "json",
        success: function (data) {
            $("#tracking").html(data[0].countNum);//跟踪中
            $("#trackingCompletion").html(data[1].countNum);//跟踪完成
        }
    });
};

ConsultantReserve.tracking = function () {//今日回访
    $('#ConsultantReserveTable').bootstrapTable('hideColumn', 'revisit');
    document.getElementById('isDone').value="待回访";
    ConsultantReserve.search();
    $("#toTracking").addClass('actionClick').siblings().removeClass('actionClick');
};

ConsultantReserve.trackingCompletion = function () {//已回访
    $('#ConsultantReserveTable').bootstrapTable('showColumn', 'revisit');
    document.getElementById('isDone').value="已回访";
    ConsultantReserve.search();
};

ConsultantReserve.search = function () {
    ConsultantReserve.table.refresh({query: ConsultantReserve.formParams()});
    ConsultantReserve.totalNum();
};

var parseParam = function (param) {//把对象拼接成URL的键值对参数param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

ConsultantReserve.downLoad = function () {
    var queryData = {};
    queryData['reach'] = parent.$("#doctorTcInputPublic").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    queryData['diagnosisStatus'] = $("#diagnosisStatus").val();
    queryData['isDone'] = $("#isDone").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/reserve/consultantExport?' + param;
};

ConsultantReserve.resetSearch = function () {
    $("#followStatus").val("");
    $("#dealStatus").val("");
    $("#fieldTc").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#diagnosisStatus").val("");
    $("#consumptionStatus").val("");
    parent.$("#doctorTcInputPublic").val("");
    $(".timeBox > div button").removeClass('actionClick');
    ConsultantReserve.search();
};

ConsultantReserve.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['reach'] = parent.$("#doctorTcInputPublic").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['isDone'] = $("#isDone").val();
    queryData['diagnosisStatus'] = $("#diagnosisStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    return queryData;
};

ConsultantReserve.today = function () {// 今日
    $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    ConsultantReserve.search();
};

ConsultantReserve.yesterday = function () {// 昨日
    $("#startTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    ConsultantReserve.search();
};

ConsultantReserve.thisWeek = function () {// 本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    $(".timeBox > div button").eq(2).addClass('actionClick').siblings().removeClass('actionClick');
    ConsultantReserve.search();
};

$("#doctorTcSearchAll", parent.document).click(function(){//主页搜索
    ConsultantReserve.search();
});

$(function () {
    var defaultColunms = ConsultantReserve.initColumn();
    var table = new BSTable(ConsultantReserve.id, "/reserve/resList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ConsultantReserve.formParams());
    ConsultantReserve.table = table.init();
    ConsultantReserve.tracking();
    menuNameHideColumn($("#menuName").val(),'ConsultantReserveTable');
    var seletefieldTc = $('#fieldTcValues').val();
    if (seletefieldTc != "") {$("#fieldTc option[value='" + seletefieldTc + "']").attr("selected", "selected");ConsultantReserve.today();}
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {ConsultantReserve.search();});
    checkDate = function () {ConsultantReserve.search();};
});